package edu.aynu.controller;

import edu.aynu.entity.Judge;
import edu.aynu.service.EsJudgeService;
import edu.aynu.service.JudgeService;
import edu.aynu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.annotation.Resource;

/**
 * (Judge)表控制层
 *
 * @author haha
 * @since 2021-05-20 14:39:14
 */
@RestController
public class JudgeController {
    /**
     * 服务对象
     */
    @Resource
    private JudgeService judgeService;

    @Autowired
    private EsJudgeService esJudgeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("judge")
    public Result selectOne(Integer id) {
        Judge judge = this.judgeService.queryById(id);
        if (judge == null) {
            return Result.fail();
        }
        return Result.success().add("judge", judge);
    }

    /**
     * 通过实体作为筛选条件查询
     */
    @GetMapping("judges")
    public Result queryAll(@RequestBody Judge judge) {
        List<Judge> list = esJudgeService.queryBySubject(judge);
        //List<Judge> list = this.judgeService.queryAll(judge);
        if (list.size() == 0) {
            return Result.fail();
        }
        return Result.success().add("judges", list);
    }

    /**
     * 新增数据
     */
    @PostMapping("judge")
    public Result insert(@RequestBody Judge judge) {
        int i = this.judgeService.insert(judge);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 修改数据
     */
    @PutMapping("judge")
    public Result update(@RequestBody Judge judge) {
        int i = this.judgeService.update(judge);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @DeleteMapping("judge")
    public Result deleteById(Integer id) {
        boolean b = this.judgeService.deleteById(id);
        if (b == false) {
            return Result.fail();
        }
        return Result.success();
    }
}
